package com.v9.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.constant.UserConstants;
import com.v9.common.dto.MenuTreeDto;
import com.v9.common.exception.BizException;
import com.v9.common.utils.StringUtils;
import com.v9.common.web.AbstractController;
import com.v9.system.domain.MenuInfo;
import com.v9.system.domain.RoleMenu;
import com.v9.system.mapper.RoleMenuMapper;
import com.v9.system.service.MenuInfoService;
import com.v9.system.service.RoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping(value = "/system/menu", method = RequestMethod.POST)
public class SysMenuController extends AbstractController {
    @Autowired
    private MenuInfoService menuService;

    @Resource
    private RoleInfoService roleInfoService;

    @Resource
    private RoleMenuMapper roleMenuMapper;
    /**
     * 获取菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @RequestMapping("/list")
    public List<MenuTreeDto> list() {
        return menuService.buildTreeByList(menuService.queryAll());
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @RequestMapping(value = "/view/{id}")
    public MenuInfo view(@PathVariable Long id) {
        return menuService.findById(id);
    }


    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @RequestMapping(value = "add")
    public void add(@Validated @RequestBody MenuInfo menu) {
        MenuInfo entity = new MenuInfo();
        entity.setName(menu.getName());
        entity = menuService.findByOne(entity);
        if (entity != null) {
            throw new BizException("新增菜单'" + menu.getName() + "'失败，菜单名称已存在");
        }
        if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
            throw new BizException("新增菜单'" + menu.getName() + "'失败，地址必须以http(s)://开头");
        }
        menuService.insert(menu);
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @RequestMapping(value = "save")
    public void save(@RequestBody MenuInfo menu) {
        MenuInfo entity = new MenuInfo();
        entity.setName(menu.getName());
        List<MenuInfo> list = menuService.query(entity);
        if (list.size() > 0 && !menu.getId().equals(list.get(0).getId())) {
            throw new BizException("修改菜单'" + menu.getName() + "'失败，菜单名称已存在");
        }
        if (menu.getIsFrame() && !StringUtils.ishttp(menu.getPath())) {
            throw new BizException("修改菜单'" + menu.getName() + "'失败，地址必须以http(s)://开头");
        }
        if (menu.getId().equals(menu.getParentId())) {
            throw new BizException("修改菜单'" + menu.getName() + "'失败，上级菜单不能选择自己");
        }
        menuService.updateById(menu);
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @RequestMapping("/remove/{id}")
    public void remove(@PathVariable("id") Long id) {
        MenuInfo entity = new MenuInfo();
        entity.setParentId(id);
        Long aLong = menuService.selectCount(entity);
        if (aLong.longValue()>0) {
            throw new BizException("存在子菜单,不允许删除");
        }
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",id);
        aLong=roleMenuMapper.selectCount(queryWrapper);
        if (aLong.longValue()>0) {
            throw new BizException("菜单已分配,不允许删除");
        }
        menuService.remove(id);
    }
}